package yxy.game.pm2.command.mail;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.MailMessage;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

/** 邮件 邮件删除已读 */
@MessageType(proto = Command.MailRemoveReaded, classtype = MailMessage.MailRemoveReadedRequest.class)
public class CMD_MailRemoveReaded extends Command<MailMessage.MailRemoveReadedRequest> {

	@Override
	public void execute(MessageSource messageSource, MailMessage.MailRemoveReadedRequest request) throws RedisException {
		String playerCode = messageSource.getTag();
		MailTypeEnum type = MailTypeEnum.forNumber(request.getType().getNumber());

		MailMessage.MailRemoveReadedResponse.Builder response = MailMessage.MailRemoveReadedResponse.newBuilder();

		MailHandle mailHandle = new MailHandle();

		mailHandle.removeReaded(playerCode, type);
		response.addAllMails(mailHandle.packMails(playerCode));
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, MailMessage.MailRemoveReadedResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}